package com.javaspeak.designpatterns.go4.structural.composite;


/**
 * Extends AbstractHtmlElement to provide a composite implementation for a
 * Html table.  An html table is considered a composite object as it can
 * itself contain composite objects such as other html tables.
 * <p>
 * TableElement wraps the child HtmlElements in "tr" and "td" html tags.
 *
 * @author John Dickerson
 */
public class TableElement extends AbstractHtmlElement {

    /* (non-Javadoc)
     * @see com.javaspeak.designpatterns.go4.structural.composite.
     *      AbstractHtmlElement#getHtml()
     */
    @Override
    public String getHtml() {

        if ( htmlElements != null ){
            StringBuilder sb = new StringBuilder();

            sb.append( "<table cellspacing=\"5\" cellpadding=\"5\">\n" );

            for ( HtmlElement pageElement : htmlElements ){
                sb.append( "<tr><td>" );
                sb.append( pageElement.getHtml() ).append( "</td></tr>\n" );
            }

            sb.append( "</table>\n" );
            return sb.toString();
        }

        return "";
    }
}
